package com.wonderful.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.wonderful.common.enums.BizCodeEnum;
import com.wonderful.mall.member.exception.MobileExistException;
import com.wonderful.mall.member.exception.UsernameExistException;
import com.wonderful.mall.member.feign.CouponFeignService;
import com.wonderful.mall.member.vo.MemberLoginVo;
import com.wonderful.mall.member.vo.MemberRegisterVo;
import com.wonderful.mall.member.vo.SocialUser;
import kotlin.jvm.Volatile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wonderful.mall.member.entity.MemberEntity;
import com.wonderful.mall.member.service.MemberService;
import com.wonderful.common.utils.PageUtils;
import com.wonderful.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author BLITE
 * @email 1476995949@qq.com
 * @date 2022-09-10 12:57:30
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberService memberService;
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser vo) throws Exception {
        MemberEntity entity = memberService.login(vo);
        if(entity!=null)
            //TODO 登录成功后的处理
            return R.ok().setData(entity);
        return R.error(BizCodeEnum.LOGIN_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGIN_PASSWORD_INVALID_EXCEPTION.getMsg());
    }
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity memberEntity = memberService.login(vo);
        if(memberEntity!=null)
            return R.ok().setData(memberEntity);
        return R.error(BizCodeEnum.LOGIN_PASSWORD_INVALID_EXCEPTION.getCode(),BizCodeEnum.LOGIN_PASSWORD_INVALID_EXCEPTION.getMsg());
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (MobileExistException e) {
            return R.error(BizCodeEnum.MObILE_EXIST_EXCEPTION.getCode(),BizCodeEnum.MObILE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
